package org.iteam.xlz.core.service.base;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.iteam.commons.utils.DateUtil;
import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.terminal.TerminalType;

public class TerminalTypeService extends BaseService<TerminalType>{
	static TerminalTypeService instance;
	private TerminalTypeService(){}
	
	public static TerminalTypeService getInstance(){
		if(null==instance){
			instance=new TerminalTypeService();
		}
		return instance;
	}
	
	public String getTypeParent(String tid){
		String s="";
		if(null==tid){
			return "";
		}
		try {
			TerminalType t=super.show("select r.* from terminal l left join terminaltype r on l.ttid=r.ttid where 1=1 and l.tid=?",tid);
			if(StringUtils.isNotBlank(t.getTypeparent())){
				s=t.getTypeparent();
			}
			return s;
		} catch (SQLException e) {
			e.printStackTrace();
			return s;
		}
	}
	
	public List<TerminalTree> buildTerminalTree(){
		try {
			List<TerminalType> all=super.query("select * from terminaltype");
			List<TerminalTree> list=new ArrayList<TerminalTree>();
			List<TerminalTree> secondList=new ArrayList<TerminalTree>();
			
			for(TerminalType a:all){//设置一级区域
				if(null==a.getTypeparent()){
					TerminalTree at=new TerminalTree();
					at.setId(a.getTtid());
					at.setText(a.getTypename());
					list.add(at);
				}
			}
			
			for(TerminalTree at:list){//设置二级区域
				List<TerminalTree> children=new ArrayList<TerminalTree>();
				for(TerminalType a:all){
					if(at.getId().equals(a.getTypeparent())){
						TerminalTree ati=new TerminalTree();
						ati.setId(a.getTtid());
						ati.setText(a.getTypename());
						children.add(ati);
						secondList.add(ati);//这里面保存的全是二级区域
					}
				}
				at.setChildren(children);
			}
			
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<TerminalTree>();
		}
	}
	
	public long counts(String typeparent){
		StringBuilder sql=new StringBuilder("select count(1) from terminaltype where 1=1 ");
		List<Object> params=new ArrayList<Object>();
		if(StringUtils.isNotBlank(typeparent)){
			sql.append(" and typeparent=?");
			params.add(typeparent);
		}else{
			sql.append(" and typeparent is null");
		}
		try {
			return super.counts(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<TerminalType> query(String typeparent,long page,long rows){
		StringBuilder sql=new StringBuilder("select * from terminaltype where 1=1 ");
		List<Object> params=new ArrayList<Object>();
		if(StringUtils.isNotBlank(typeparent)){
			sql.append(" and typeparent=?");
			params.add(typeparent);
		}else{
			sql.append(" and typeparent is null");
		}
		sql.append(" limit ?,?");
		params.add((page-1)*rows);
		params.add(rows);
		try {
			return super.query(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<TerminalType>();
		}
	}
	
	public boolean create(TerminalType model){
		SqlBuilder sql=SqlBuilder.createInsert("terminaltype");
		sql.addField("ttid",UUID.randomUUID().toString().replace("-","").toUpperCase());
		sql.addField("typename",model.getTypename());
		if(StringUtils.isNotBlank(model.getTypeparent())){
			sql.addField("typeparent",model.getTypeparent());
		}
		sql.addField("createtime",DateUtil.currentTime());
		sql.addField("creator",model.getCreator());
		sql.addField("status",model.getStatus());
		try {
			return super.updates(sql.getSql(),sql.getParams())>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean update(TerminalType model){
		SqlBuilder sql=SqlBuilder.createUpdate("terminaltype");
		sql.addField("typename",model.getTypename());
		sql.addField("status",model.getStatus());
		sql.addWhere("ttid",model.getTtid());
		try {
			return super.updates(sql.getSql(),sql.getParams())>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean delete(String ttid){
		try {
			return super.updates("delete from terminaltype where 1=1 and ttid=?",ttid)>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public TerminalType findById(String ttid){
		try {
			return super.show("select * from terminaltype where 1=1 and ttid=?",ttid);
		} catch (SQLException e) {
			e.printStackTrace();
			return new TerminalType();
		}
	}
	
	public class TerminalTree{
		String id;
		String text;
		List<TerminalTree> children;
		public String getId() {
			return id;
		}
		public void setId(String id) {
			this.id = id;
		}
		public String getText() {
			return text;
		}
		public void setText(String text) {
			this.text = text;
		}
		public List<TerminalTree> getChildren() {
			return children;
		}
		public void setChildren(List<TerminalTree> children) {
			this.children = children;
		}
		
	}	
}
